package com.project.service;

import com.project.common.utils.Constants;
import com.project.entity.TeacherInfo;
import com.project.entity.User;
import com.project.entity.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: TateBrown
 * @date: 2018/5/26 15:12
 * @param:
 * @return:
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private UserService userService;

    @Autowired
    private TeacherInfoService teacherInfoService;

    @Autowired
    private UserRoleService userRoleService;

    /**
     * author: WheelChen
     * date: 2017/10/24
     * function: 创建教师账号
     *
     * @param user
     * @param teacherInfo
     * @return
     */
    @Override
    public boolean addDetailOne(User user, TeacherInfo teacherInfo) throws Exception {
        if (user != null) {
            if (userService.checkUsernameAndUniversityIdIsExist(user.getUsername(), user.getUniversityid()))
                return false;
            userService.createUser(user);
            if (user.getId() == null)
                throw new Exception("获取用户id失败");
            if (teacherInfo != null){
                teacherInfo.setUserid(user.getId());
                teacherInfoService.add(teacherInfo);
            }
            else {
                TeacherInfo teacherInfo1 = new TeacherInfo();
                teacherInfo1.setUserid(user.getId());
                teacherInfoService.add(teacherInfo1);
            }
            //用户权限表插入
            UserRole userRole = new UserRole();
            userRole.setUserid(user.getId());
            userRole.setRoleid(Constants.USERTYPE.TEACHER.value());
            userRoleService.add(userRole);
        } else {
            throw new Exception("用户表不存在");
        }
        return true;
    }
}
